package com.gxwzxy.medic.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.gxwzxy.medic.dao.MenuDao;
import com.gxwzxy.medic.entity.Menu;
import com.gxwzxy.medic.entity.otherentity.MenuTree;
import com.gxwzxy.medic.until.Connect;

public class MenuDaoImpl implements MenuDao {

	// 全部不安全变量
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps = null;
	
	//查询menu,封装成MenuTree
	@Override
	public List<MenuTree> TreeShow() {
		//返回的数据list
		ArrayList<MenuTree> am = new ArrayList<MenuTree>();
		String sql = null;
		try {
			//查询的sql语句
			sql = "select * from menu";
			//执行有result返回的方法，sql传入
			rs = Connect.executeF(sql);
			//遍历封装数据到list
			while(rs.next()) {
				MenuTree mt = new MenuTree(rs.getString(2),rs.getString(3),rs.getString(4),"right",false);
			    am.add(mt);
			}
		}catch(Exception e){
			e.printStackTrace();
		}//关闭连接，帮助gc回收
		finally {
			Connect.close(conn, ps, rs);
		}
		return am;
	}

	/**
	 * 閫氳繃鐢ㄦ埛缂栧彿鏌ユ壘鏉冮檺缂栧彿
	 */
	@Override
	public List<Menu> findMenuByUserId(String userId) {
		//澹版槑鍙橀噺
		ArrayList<Menu> am = new ArrayList<Menu>();	
		
		String sql = "select menu_id from role_menu where role_id ="
		            +"(select role_id from  user_role where user_id = ?)";
		rs = Connect.executeF(sql, userId);
		try {
			
			while(rs.next()) {
				Menu m = new Menu();
				m.setMenuId(rs.getString(1));
				am.add(m);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return am;
		
		
	}
	
	/**
	 *获取全部的menu数据
	 */
	@Override
	public List<Menu> findMenu() {
		//获取全部的数据
		ArrayList<Menu> am = new ArrayList<Menu>();	
		String sql = "select * from menu";
		rs = Connect.executeF(sql);
		try {
			while(rs.next()) {
				Menu m = new Menu(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
				am.add(m);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return am;
	}

	/**
	 * 获取menu的全部记录数
	 */
	@Override
	public int findMenuSize() {
		String sql = "select COUNT(*) from menu";
		int i = 0;
		try {
			//执行sql语句
			rs = Connect.executeF(sql);
			if(rs.next()) {
				i = rs.getInt(1);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return i;
	}
	/**
	 * 根据传入的下标和查询数据长度来查询数据
	 */
	@Override
	public List<Menu> findMenuIndex(int start, int size) {
		// 定义集合和sql语句
		ArrayList<Menu> am = new ArrayList<>();
		String sql = null;
		try {
			// 定义Sql语句
			sql = "select * from menu limit ?,?";
			// 执行sql获得返回的数据
			rs =Connect.executeF(sql, start,size);
			// 数据封装
			while (rs.next()) {
				Menu m = new Menu(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5));
				am.add(m);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Connect.close(conn, ps, rs);
		}
		// 返回数据
		return am;
	}

	/**
	 * 娣诲姞鏉冮檺
	 */
	@Override
	public boolean addMenu(Menu m) {
		//澹版槑鍙橀噺
		String sql = "insert into menu(menu_id,menu_name,menu_pid,url) value(?,?,?,?)";
		int i = -1;
		try {
			i = Connect.executeDML(sql, m.getMenuId(),m.getMenuName(),m.getMenupId(),m.getUrl());
		}catch(Exception e) {
			e.printStackTrace();
		}
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据menuId一条具体的数据
	 */
	@Override
	public List<Menu> findMenuBymenuId(String menuId) {
		//构造一条具体的sql语句
		String sql = "select * from menu where menu_id = ?";
		ArrayList<Menu> lm = new ArrayList<Menu>();
		try {
			//执行sql语句，获取返回值
			rs = Connect.executeF(sql, menuId);
			//数据封装
			while(rs.next()) {
				Menu m = new Menu(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
				lm.add(m);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return lm;
	}

	/**
	 * 閫氳繃id淇敼鏉冮檺
	 */
	@Override
	public boolean updateMenu(Menu m,String rmenuId) {
		String sql = "update role_menu set menu_id = ? where menu_id = ?";//淇敼role_menu琛ㄧ殑
		String msql = "update menu set menu_id = ?,menu_name=?,menu_pid=?,url=? where menu_id = ?";//淇敼menu琛ㄧ殑
		int i = -1;
		int j = -1;
		//璋冪敤Connect閲岀殑鏂规硶
		i = Connect.executeDML(sql, m.getMenuId(),rmenuId);
		j = Connect.executeDML(msql, m.getMenuId(),m.getMenuName(),m.getMenupId(),m.getUrl(),rmenuId);
		if(i!=-1&&j!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据menuId删除menu和 role_menu
	 */
	@Override
	public boolean MenuDelete(String menuId) {
		String sql = "delete from menu where menu_id = ?";//构造删除menu的sql
		String rsql ="delete from role_menu where menu_id = ?";//构造删除role_menu的sql语句
		int i = -1;
		int j = -1;
		//执行sql
		i = Connect.executeDML(sql, menuId);
		j = Connect.executeDML(rsql, menuId);
		if(i!=-1&&j!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 查询出对应角色应有的menu_id从ole_menu查找
	 */
	@Override
	public List<Menu> findMenuByroleId(String roleId) {
		String sql = "select * from role_menu where role_id = ?";
		ArrayList<Menu> am = new ArrayList<Menu>();
		try {
			//传入sql和占位数据
			rs = Connect.executeF(sql,roleId);
			//装入对象menu
			while(rs.next()) {
				Menu m = new Menu();
				m.setMenuId(rs.getString("menu_id"));
				am.add(m);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return am;
	}
	
	/**
	 * 鏍规嵁roleId淇敼role_menu鍏宠仈琛�
	 */
	@Override
	public boolean updateMenuByroleId(String roleId, String[] menuId) {
		//澹版槑鍙橀噺
		String dsql = "delete from role_menu WHERE role_id = ?";
		String isql = "insert into role_menu(role_id,menu_id) value(?,?)";
		int i = -1;
		i = Connect.executeDML(dsql, roleId);
		int j = -1;
		for(int m = 0;m<menuId.length;m++) {
			j = Connect.executeDML(isql, roleId,menuId[m]);
		}
		if(i!=-1&&j!=-1) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 根据menupId查询二级菜单
	 */
	@Override
	public List<Menu> findSecondMenu(String menupId) {
		//构造sql语句
		String sql = "select * from menu where menu_pid = ?";
		ArrayList<Menu> am = new ArrayList<Menu>();
		try {
			//查询
			rs = Connect.executeF(sql,menupId);
			//封装数据
			while(rs.next()) {
				Menu m = new Menu(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
				am.add(m);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return am;
	}

	
	public static void main(String[] args) {
		MenuDao md = new MenuDaoImpl();
		List<Menu> lm = md.findMenuByroleId("1");
		for(int i = 0;i<lm.size();i++) {
			System.out.println(lm.get(i).getMenuId());
		}
		
	}


	
}
